
#include <boost/algorithm/string.hpp>
#include "WebKit.h"
#include "WebPage.h"
#include "WebElement.h"

using namespace SpiderWebKitClient;
using namespace std;


void downloadTest()
{
    WebKit* wk = new WebKit("test");
    int error = 0;
    WebPage *page = wk->getWebPage(error, "http://guide.qyer.com/melbourne/");
    if (!page)
        std::cout << "get page failed!" << std::endl;

    std::string login_user_css = "#qyer_head_username";
    if (page->waitForCssSelector(login_user_css, 1000)) {
        WebElement element = page->getElementByCssSelector(login_user_css);
        std::cout << element.getOuterXml() << std::endl;
        std::cout << std::endl;
    }

    std::string resource_css = ".down.jsdownaction";

    CookieJar jar = page->getCookieJar();

    std::string cookieHeader = "Cookie : " + jar.toString();
    std::cout << cookieHeader << std::endl;
    std::cout << std::endl;

    if (page->waitForCssSelector(resource_css, 1000)) {
        std::list<WebElement> elements = page->getElementsByCssSelector(resource_css);
        std::list<WebElement>::iterator element;
        std::string link;
        std::map<std::string, std::string> headers;

        for (element = elements.begin(); element != elements.end(); element++) {
            std::cout << element->getOuterXml() <<  std::endl;
            std::cout << std::endl;
            page->download((*element), link, headers, 30000);

            std::cout << link << std::endl;
            std::cout << std::endl;
            std::cout << std::endl;

            sleep(1);
            if (page->waitForCssSelector(".ui_pupBox_close", 10)) {
                WebElement closeElement = page->getElementByCssSelector(".ui_pupBox_close");
                closeElement.click();
            }
            sleep(1);
        }

        std::map<std::string, std::string>::iterator it;
        for (it = headers.begin(); it != headers.end(); it++) {
            std::cout << it->first <<  " : " << it->second << std::endl;
        }
    }
    delete wk;
}

void downloadTest2()
{
    //WebKit* wk = new WebKit("test", true);
    WebKit* wk = new WebKit("wshpc");
    int error = 0;
    WebPage *page = wk->getWebPage(error, "http://www.nodejs.org/download/");
    //WebPage *page = wk->getWebPage(error, "http://www.baidu.com/");
    if (!page)
        std::cout << "get page failed!" << std::endl;
    std::cout << page->getTitle() << std::endl;

    std::string css = "#source > a:nth-child(1)";
    if (page->waitForCssSelector(css, 1000)) {
        WebElement element = page->getElementByCssSelector(css);
        std::cout << element.getOuterXml() << std::endl;
        std::string link;
        std::map<std::string, std::string> header;
        page->download(element, link, header, 30000);
        std::cout << link << std::endl;
    }
    delete wk;
}

void selectTest()
{
    WebKit* wk = new WebKit("tcp://10.144.34.0:21225", true, WKMethodTypeStaticProtocol);
    int error = 0;
    WebPage *page = wk->getWebPage(error, "http://www.woqu.com/australia-ticket-detail-via_bf_ceicsohg");
    if (!page)
        std::cout << "get page failed!" << std::endl;

    std::string title = page->getTitle();
    std::cout << title << std::endl;

    std::string css = "#package";
    if (page->waitForCssSelector(css, 1000)) {
        WebElement price = page->getElementByCssSelector("#total-price");
        std::cout << price.getText() << std::endl;
        WebElement element = page->getElementByCssSelector(css);
        element.selectOptionByIndex(0);
        std::cout << price.getText() << std::endl;
    }
}

